package com.yj.ccs.abs.web.management.mapper;

import com.yj.ccs.abs.web.management.dto.IdListDto;
import com.yj.ccs.abs.web.management.entity.Client;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

/**
 * Copyright 2018 (C) Yunjian-VC
 * Created on : 2018/3/16
 * Author     : Unbelievable9
 **/
@Repository
@Mapper
public interface ClientMapper {

    @Options(useGeneratedKeys = true, keyProperty = "item.id")
    @Insert("INSERT INTO client (`name`, short_name, area_code, address, industry, nature, " +
            "representative, parent_id, final_parent_id, shadow_rating, credit_day, create_time) " +
            "VALUES (#{item.name}, #{item.short_name}, #{item.area_code}, #{item.address}, " +
            "#{item.industry}, #{item.nature}, #{item.representative}, #{item.parent_id}, " +
            "#{item.final_parent_id}, #{item.shadow_rating}, #{item.credit_day}, NOW())")
    void create(@Param("item") Client item);

    @Update("UPDATE `client` SET `name` = #{item.name}, short_name = #{item.short_name}, area_code = #{item.area_code}, address = #{item.address}, " +
            "industry = #{item.industry}, nature = #{item.nature}, representative = #{item.representative}, parent_id = #{item.parent_id}, " +
            "final_parent_id = #{item.final_parent_id}, shadow_rating = #{item.shadow_rating}, credit_day = #{item.credit_day} " +
            "WHERE id = #{item.id}")
    void update(@Param("item") Client item);

    @Delete("<script>" +
            "DELETE FROM client WHERE id IN (" +
            "<foreach collection = 'item.id_list' item = 'id' separator = ', '>" +
            "#{id}" +
            "</foreach>" +
            ")" +
            "</script>")
    void batchDelete(@Param("item") IdListDto item);

    @Select("SELECT * FROM client WHERE id = #{id} LIMIT 1")
    Client getById(@Param("id") Long id);

    @Select("SELECT * FROM client WHERE name = #{name} LIMIT 1")
    Client getByName(@Param("name") String name);
}
